package org.liaohailong.helloworld.practicemiddle;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Author: liaohailong
 * Time: 2021/7/7 11:01
 * Describe: 中级算法 - 树
 */
public class TreePractice {
    public static final class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static final class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
            next = _next;
        }
    }

    // LC 二叉树的中序遍历
    public List<Integer> inorderTraversal(TreeNode root) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        _inorderTraversal(root, linkedList);
        return linkedList;
    }

    private void _inorderTraversal(TreeNode node, List<Integer> result) {
        if (node == null) return;

        _inorderTraversal(node.left, result);
        result.add(node.val);
        _inorderTraversal(node.right, result);
    }


    // LC 二叉树的锯齿形层次遍历
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        if (root == null) return new LinkedList<>();
        List<List<Integer>> result = new LinkedList<>();
        LinkedList<TreeNode> bfs = new LinkedList<>();
        bfs.add(root);
        int depth = -1;
        while (!bfs.isEmpty()) {
            int size = bfs.size();
            LinkedList<Integer> list = new LinkedList<>();
            depth++;
            boolean fromLeft = depth % 2 == 0;
            while (size-- > 0) {
                TreeNode treeNode = bfs.pollFirst();
                if (fromLeft)
                    list.addLast(treeNode.val);
                else
                    list.addFirst(treeNode.val);

                if (treeNode.left != null) bfs.add(treeNode.left);
                if (treeNode.right != null) bfs.add(treeNode.right);
            }
            result.add(list);
        }

        return result;
    }

    // LC 从前序与中序遍历序列构造二叉树
//    前序遍历 preorder = [3,9,20,15,7] 根左右
//    中序遍历 inorder = [9,3,15,20,7]  左根右
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        Map<Integer, Integer> map = new HashMap<>();   //建立结点值和结点索引的映射
        for (int i = 0; i < inorder.length; i++) {
            map.put(inorder[i], i);
        }
        return _buildTree(
                preorder, 0, preorder.length - 1,
                inorder, 0, inorder.length - 1,
                map);
    }

    private TreeNode _buildTree(int[] preorder, int pre_left, int pre_right,
                                int[] inorder, int in_left, int in_right,
                                Map<Integer, Integer> map) {
        if (pre_left > pre_right) return null;

        int pre_root = preorder[pre_left]; // 当前节点的值
        int in_root = map.get(pre_root); // 当前节点，中序位置，小于这个index的都在左子树上
        int left_size = in_root - in_left; // 左子树的数量

        TreeNode node = new TreeNode(pre_root);

        node.left = _buildTree(preorder, pre_left + 1, pre_left + left_size, inorder, in_left, in_root - 1, map);
        node.right = _buildTree(preorder, pre_left + left_size + 1, pre_right, inorder, in_root + 1, in_right, map);

        return node;
    }

    // LC 填充每个节点的下一个右侧节点指针
    // 完美二叉树
//    public Node connect(Node root) {
//        if (root == null) return root;
//        _connect(root.left, root.right, root);
//        return root;
//    }
//
//    private void _connect(Node left, Node right, Node parent) {
//        if (left != null) left.next = right;
//        if (right != null)
//            right.next = parent.next == null ? null :
//                    parent.next.left != null ? parent.next.left : parent.next.right;
//        if (right != null) _connect(right.left, right.right, right);
//        if (left != null) _connect(left.left, left.right, left);
//    }

    // LC 填充每个节点的下一个右侧节点指针
    // 非完美二叉树
    public Node connect(Node root) {
        if (root == null) return root;
        if (root.left != null) {
            if (root.right != null) {
                root.left.next = root.right;
            } else root.left.next = getNext(root.next);
        }
        if (root.right != null) {
            root.right.next = getNext(root.next);
        }
        connect(root.right);
        connect(root.left);
        return root;
    }

    private Node getNext(Node uncle) {
        if (uncle == null) return null;
        if (uncle.left != null) return uncle.left;
        if (uncle.right != null) return uncle.right;
        return getNext(uncle.next);
    }

    // LC 二叉搜索树中第K小的元素
    public int kthSmallest(TreeNode root, int k) {
        LinkedList<Integer> result = new LinkedList<>();
        _kthSmallest(root, k, result);
        return result.getLast();
    }

    private void _kthSmallest(TreeNode root, int k, LinkedList<Integer> result) {
        if (root == null) return;

        _kthSmallest(root.left, k, result);
        if (result.size() >= k) return;
        result.add(root.val);
        _kthSmallest(root.right, k, result);
    }

    // LC 岛屿数量
    public int numIslands(char[][] grid) {
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    _numIslands(grid, i, j);
                }
            }
        }
        return count;
    }

    private void _numIslands(char[][] grid, int i, int j) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[i].length) return;

        boolean remove = grid[i][j] == '1';

        if (remove) {
            grid[i][j] = '0';
            _numIslands(grid, i - 1, j); // 上
            _numIslands(grid, i + 1, j); // 下
            _numIslands(grid, i, j - 1); // 左
            _numIslands(grid, i, j + 1); // 右
        }
    }

}
